//Copyright 2012 Trevin Liberty
//GNU GPL v3
//Author: Trevin Liberty

#include <iostream>
#include <string>
#include <sstream>

#include "Allegro_Misc.h"

using std::string;
using std::ostringstream;
using std::cout;
using std::endl;

int allegroInit(int WINDOW_WIDTH, int WINDOW_HEIGHT, int MAX_FPS, ALLEGRO_DISPLAY* &display, 
	ALLEGRO_EVENT_QUEUE* &event_queue, ALLEGRO_TIMER* &timer){

	if(!al_init())
		return -1;

	ALLEGRO_MONITOR_INFO* info = new ALLEGRO_MONITOR_INFO;
	al_get_monitor_info(0, info);
	WINDOW_WIDTH = info->x2 - 128;
	WINDOW_HEIGHT = info->y2 - 128;

	if(!al_init_primitives_addon())
		return -1;
	//al_set_new_display_flags(ALLEGRO_FULLSCREEN_WINDOW);
	display = al_create_display(WINDOW_WIDTH, WINDOW_HEIGHT);

	if(!display) {
		cout << "failed to initialize the display!\n";
		return -1;
	}

	if(!al_install_keyboard()) {
      cout << "failed to initialize the keyboard!\n";
      return false;
	}

	if(!al_install_mouse()) {
      cout << "failed to initialize the mouse!\n";
      return false;
	}

	if(!al_init_image_addon()) {
      cout << "failed to initialize the image addon!\n";
      return false;
	}

	al_init_font_addon();
	al_init_ttf_addon();

	event_queue = al_create_event_queue();
	timer = al_create_timer(1.0 / MAX_FPS);

	srand(time(NULL));

	al_register_event_source(event_queue, al_get_display_event_source(display));
	al_register_event_source(event_queue, al_get_timer_event_source(timer));
	al_register_event_source(event_queue, al_get_mouse_event_source());
	al_register_event_source(event_queue, al_get_keyboard_event_source());

	return true;
}

void loadImages(imageArray &base, imageArray &fringe) {
	string file;
	ostringstream oss;

	int baseT = 14;
	int path = 15;
	base.arraySize = baseT + path;

	int numCastle = 14;
	int numCastleWalk = 8;
	int numCastleGate = 1;
	int numStairs = 4;
	int numWindmill = 1;
	int numFirePits = 2;
	int numHut = 3;
	int numPub = 1;
	fringe.arraySize = numCastle + numCastleWalk +
		numCastleGate + numStairs + numWindmill + 
		numFirePits + numHut + numPub;

	base.images = new bitmapImage[base.arraySize];
	fringe.images = new bitmapImage[fringe.arraySize];

///////////////////////Load Base Tiles

	int j = 0;

	for(int i = 0; i < baseT; i++) {
		oss << "rsc/Tiles/Base/tile " << i << ".bmp";
		file = oss.str();
		oss.str("");
		base.images[j++].image = al_load_bitmap(file.c_str());
		base.images[j-1].sizeX = al_get_bitmap_width(base.images[j-1].image);
		base.images[j-1].sizeY = al_get_bitmap_height(base.images[j-1].image);
	}

	for(int i = 0; i < path; i++) {
		oss << "rsc/Tiles/Path/path " << i << ".tga";
		file = oss.str();
		oss.str("");
		base.images[j++].image = al_load_bitmap(file.c_str());
		base.images[j-1].sizeX = al_get_bitmap_width(base.images[j-1].image);
		base.images[j-1].sizeY = al_get_bitmap_height(base.images[j-1].image);
	}

/////////////////////Load Fringe

	j = 0;

	for(int i = 0; i < numCastle; i++) {
		oss << "rsc/Castle/castle/castle " << i << ".tga";
		file = oss.str();
		oss.str("");
		fringe.images[j++].image = al_load_bitmap(file.c_str());
		fringe.images[j-1].sizeX = al_get_bitmap_width(fringe.images[j-1].image);
		fringe.images[j-1].sizeY = al_get_bitmap_height(fringe.images[j-1].image);
	}

	for(int i = 0; i < numCastleWalk; i++) {
		oss << "rsc/Castle/castleWalk/w_castle " << i << ".tga";
		file = oss.str();
		oss.str("");
		fringe.images[j++].image = al_load_bitmap(file.c_str());
		fringe.images[j-1].sizeX = al_get_bitmap_width(fringe.images[j-1].image);
		fringe.images[j-1].sizeY = al_get_bitmap_height(fringe.images[j-1].image);
	}

	for(int i = 0; i < numStairs; i++) {
		oss << "rsc/Castle/castleStair/stair " << i << ".tga";
		file = oss.str();
		oss.str("");
		fringe.images[j++].image = al_load_bitmap(file.c_str());
		fringe.images[j-1].sizeX = al_get_bitmap_width(fringe.images[j-1].image);
		fringe.images[j-1].sizeY = al_get_bitmap_height(fringe.images[j-1].image);
	}

	file = "rsc/Castle/castleGate/castleGate.tga";
	fringe.images[j++].image = al_load_bitmap(file.c_str());
	fringe.images[j-1].sizeX = al_get_bitmap_width(fringe.images[j-1].image);
	fringe.images[j-1].sizeY = al_get_bitmap_height(fringe.images[j-1].image);

	fringe.images[j++].image = al_load_bitmap("rsc/Objects/Windmill/aWindmillBuilding.tga");
	fringe.images[j-1].sizeX = 288;
	fringe.images[j-1].sizeY = 288;

	fringe.images[j++].image = al_load_bitmap("rsc/Objects/FirePit/woodON.tga");
	fringe.images[j-1].sizeX = 64;
	fringe.images[j-1].sizeY = 64;

	fringe.images[j++].image = al_load_bitmap("rsc/Objects/FirePit/woodOFF.tga");
	fringe.images[j-1].sizeX = 64;
	fringe.images[j-1].sizeY = 64;

	fringe.images[j++].image = al_load_bitmap("rsc/Houses/Hut 0.tga");
	fringe.images[j-1].sizeX = 256;
	fringe.images[j-1].sizeY = 288;

	fringe.images[j++].image = al_load_bitmap("rsc/Houses/Hut 1.tga");
	fringe.images[j-1].sizeX = 256;
	fringe.images[j-1].sizeY = 288;

	fringe.images[j++].image = al_load_bitmap("rsc/Houses/Hut 2.tga");
	fringe.images[j-1].sizeX = 256;
	fringe.images[j-1].sizeY = 288;

	fringe.images[j++].image = al_load_bitmap("rsc/Houses/Pub.tga");
	fringe.images[j-1].sizeX = 256;
	fringe.images[j-1].sizeY = 288;
}